package endoscopic

import (
	"errors"
	"rongyy/src/app"
	"rongyy/src/middlewares"
	"rongyy/src/models"

	"xorm.io/xorm/log"
)

type PatientCard struct {
	models.BaseModel `xorm:"extends"`
	HealthCode       string `json:"health_code" xorm:"-"`
	PatientNo        string `json:"patient_no"`
	PatientName      string `json:"patient_name"`
	CardNo           string `json:"card_no"`
	UnionId          string `json:"union_id" xorm:"-"`
	IdNo             string `json:"id_no"`
	Mobile           string `json:"mobile"`
	Age              string `json:"age"`
	Sex              string `json:"sex"`
	EhCard           string `json:"ehcard" xorm:"ehcard"`
	Nation           string `json:"nation"`
	Occupation       string `json:"occupation"`
	MaritalStatus    string `json:"marital_status"`
}

func (pc *PatientCard) TableName() string {
	return "patient_card"
}

type PatientWxInfo struct {
	PatientId   string `xorm:"patient_no" json:"patient_id"`
	PatientName string `xorm:"patient_name" json:"patient_name"`
	Openid      string `xorm:"open_id" json:"openid"`
	ECardSn     string `xorm:"ehcard" json:"ecard_sn"`
}

func (pc *PatientCard) GetInfoByPatientId(patientId string) ([]PatientWxInfo, error) {
	app.DB().SetLogLevel(log.LOG_ERR)
	pcs := make([]PatientWxInfo, 0)
	err := app.DB().Table("patient_card").Alias("pc").
		Join("INNER", "patient_card_wx pcw", "pc.id = pcw.pc_id").
		Where("pcw.status = 1 and pc.patient_no = ?", patientId).Find(&pcs)

	if err != nil {
		middlewares.Logger.Println("ERROR - PatientCard GetInfoByPatientId failed ", err)
		return nil, errors.New("查询就诊卡信息失败")
	}
	return pcs, nil
}
